package com.ibm.commerce.order.facade.server.commands;

import com.ibm.commerce.foundation.common.util.logging.LoggingHelper;
import com.ibm.commerce.foundation.common.util.sdo.SDODataTypeHelper;
import com.ibm.commerce.foundation.server.command.CommandFactory;
import com.ibm.commerce.foundation.server.command.GenericCommandKeyImpl;
import com.ibm.commerce.foundation.server.command.bod.AbstractGetBusinessObjectDocumentCmdImpl;
import com.ibm.commerce.foundation.server.command.bod.AbstractInsertMoreNounDataCmd;
import com.ibm.commerce.foundation.server.util.oagis.SelectionCriteriaMapper;
import com.ibm.commerce.oagis9.datatypes.BusinessObjectDocumentType;
import com.ibm.commerce.oagis9.datatypes.GetType;
import com.ibm.commerce.order.facade.datatypes.ChannelIdentiferType;
import com.ibm.commerce.order.facade.datatypes.ChannelType;
import com.ibm.commerce.order.facade.datatypes.GetOrderDataAreaType;
import com.ibm.commerce.order.facade.datatypes.GetOrderType;
import com.ibm.commerce.order.facade.datatypes.OrderFactory;
import com.ibm.commerce.order.facade.datatypes.OrderType;
import com.ibm.commerce.order.facade.datatypes.ShowOrderDataAreaType;
import com.ibm.commerce.order.facade.datatypes.ShowOrderType;
import com.ibm.commerce.order.facade.datatypes.impl.OrderTypeImpl;
import com.ibm.commerce.foundation.common.datatypes.CommerceFoundationFactory;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.math.BigDecimal;


import com.ibm.commerce.foundation.common.datatypes.MonetaryAmountType;
import com.ibm.commerce.foundation.common.datatypes.OrderIdentifierType;
import com.ibm.commerce.foundation.common.datatypes.OrderChargesType;
import com.ibm.commerce.foundation.common.datatypes.PersonIdentifierType;
import com.ibm.commerce.foundation.common.datatypes.StoreIdentifierType;
import com.ibm.commerce.order.facade.datatypes.OrderStatusType;
import com.ibm.commerce.order.facade.datatypes.OrderPaymentInfoType;
import com.ibm.commerce.order.facade.datatypes.PaymentInstructionType;




public class GetOrderCmdImpl extends AbstractGetBusinessObjectDocumentCmdImpl
  implements GetOrderCmd
{
  private static final String COPYRIGHT = "(c) Copyright International Business Machines Corporation 1996,2008";
  private static final String CLASSNAME = GetOrderCmdImpl.class.getName();

  private static final Logger LOGGER = LoggingHelper.getLogger(GetOrderCmdImpl.class);

  private GetOrderType iGetOrder = null;

  private ShowOrderType iShowOrder = null;

  protected void performExpression()
    throws Exception
  {
	  
	  
	System.out.println("....GetOrderType....");
    String METHODNAME = "performExpression()";
    if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
      LOGGER.entering(CLASSNAME, "performExpression()");
    }

    try
    {
      GetOrderType bodGETOrder = getGetOrder();
      SelectionCriteriaMapper selectionCriteria = null;
      try {
        selectionCriteria = new SelectionCriteriaMapper(bodGETOrder.getDataArea().getGet().getExpression());
      } catch (Exception e) {
        selectionCriteria = null;
      }
      String xpathKey = null;
      if (selectionCriteria != null) {
        xpathKey = selectionCriteria.getXPathKey();
      }
      String commandKey = FetchOrdersCmd.class.getName();

      FetchOrdersCmd fetchOrders = (FetchOrdersCmd)CommandFactory.getInstance().createCommand(new GenericCommandKeyImpl(commandKey, xpathKey));
      System.out.println(fetchOrders.getClass().getName());
      fetchOrders.setGetOrder(getGetOrder());

      fetchOrders.execute();

      List vOrders = fetchOrders.getOrders();
      try
      {
        selectionCriteria = new SelectionCriteriaMapper(bodGETOrder.getDataArea().getGet().getExpression());
      } catch (Exception e) {
        LOGGER.logp(Level.FINE, CLASSNAME, "performExpression()", "Failed to update access profile after FetchOrdersCmd is executed");
      }

      String strAccessProfile = null;
      if (selectionCriteria != null) {
        strAccessProfile = selectionCriteria.getAccessProfile();
      }

      if ((strAccessProfile == null) || (strAccessProfile.length() == 0)) {
        strAccessProfile = "IBM_Summary";
      }

      ComposeOrderCmd composeOrder = (ComposeOrderCmd)CommandFactory.getInstance().createCommand(
        new GenericCommandKeyImpl(ComposeOrderCmd.class.getName(), strAccessProfile));
     
      System.out.println(composeOrder.getClass().getName());
      
      composeOrder.setOrders(vOrders);
      composeOrder.setPagingPrameter(fetchOrders.getPagingPrameter());
      composeOrder.setGetOrder(getGetOrder());
      composeOrder.execute();

      if (getInsertMoreDataCommandInterface() != null)
      {
        int i = 0;
        AbstractInsertMoreNounDataCmd insertMoreDataCmd = (AbstractInsertMoreNounDataCmd)CommandFactory.getInstance().createCommand(
          new GenericCommandKeyImpl(getInsertMoreDataCommandInterface(), strAccessProfile + "." + i, false));

        if (insertMoreDataCmd == null) {
          if (LoggingHelper.isTraceEnabled(LOGGER)) {
            LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL, 
              CLASSNAME, "performExpression()", "insert more data cmd with index is null, lets see if there is a default : " + insertMoreDataCmd);
          }

          insertMoreDataCmd = (AbstractInsertMoreNounDataCmd)CommandFactory.getInstance().createCommand(
            new GenericCommandKeyImpl(getInsertMoreDataCommandInterface(), strAccessProfile, false));

          if (insertMoreDataCmd != null) {
            if (LoggingHelper.isTraceEnabled(LOGGER)) {
              LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL, 
                CLASSNAME, "performExpression()", "insert more data cmd : " + insertMoreDataCmd);
            }

            insertMoreDataCmd.setGet(bodGETOrder.getDataArea().getGet());
            insertMoreDataCmd.setSearchExpression(getSearchExpression());
            insertMoreDataCmd.setNouns(composeOrder.getShowOrder().getDataArea().getOrder());
            insertMoreDataCmd.execute();
          }
        }
        else
        {
          while (insertMoreDataCmd != null) {
            if (LoggingHelper.isTraceEnabled(LOGGER)) {
              LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL, 
                CLASSNAME, "performExpression()", "found insert more data cmd for index : " + i + "[" + insertMoreDataCmd + "]");
            }

            insertMoreDataCmd.setGet(bodGETOrder.getDataArea().getGet());
            insertMoreDataCmd.setSearchExpression(getSearchExpression());
            insertMoreDataCmd.setNouns(composeOrder.getShowOrder().getDataArea().getOrder());
            insertMoreDataCmd.execute();

            i++;
            insertMoreDataCmd = (AbstractInsertMoreNounDataCmd)CommandFactory.getInstance().createCommand(
              new GenericCommandKeyImpl(getInsertMoreDataCommandInterface(), strAccessProfile + "." + i, false));
          }

          if (LoggingHelper.isTraceEnabled(LOGGER)) {
            LOGGER.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL, 
              CLASSNAME, "performExpression()", "insert more data cmd is null for index : " + i);
          }

        }

      }
 
      setShowOrder(composeOrder.getShowOrder());
    }
    finally {
      if (LoggingHelper.isEntryExitTraceEnabled(LOGGER))
        LOGGER.exiting(CLASSNAME, "performExpression()", this.iShowOrder);
    }
  }

  protected void validateExpression()
    throws Exception
  {
    String METHODNAME = "validateExpression()";
    if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) {
      Object[] param = { this.iGetOrder };
      LOGGER.entering(CLASSNAME, "validateExpression()", param);
    }

    if (LoggingHelper.isEntryExitTraceEnabled(LOGGER))
      LOGGER.exiting(CLASSNAME, "validateExpression()");
  }

  public void setGetOrder(GetOrderType argGetOrder)
  {
    this.iGetOrder = argGetOrder;
  }

  protected GetOrderType getGetOrder()
  {
    return this.iGetOrder;
  }

  public ShowOrderType getShowOrder()
  {
    return this.iShowOrder;
  }

  protected void setShowOrder(ShowOrderType aShowOrder)
  {
    this.iShowOrder = aShowOrder;
  }

  public BusinessObjectDocumentType getResultBusinessObjectDocument()
  {
    return getShowOrder();
  }

  public void setRequestBusinessObjectDocument(BusinessObjectDocumentType businessObjectDocument)
  {
    super.setRequestBusinessObjectDocument(businessObjectDocument);
    setGetOrder((GetOrderType)businessObjectDocument);
  }

  public void reset()
  {
    super.reset();
    this.iGetOrder = null;
    this.iShowOrder = null;
  }

  protected OrderFactory getOrderFactory()
  {
    return OrderFactory.eINSTANCE;
  }
}